<?php
class General_Dispatcher
{
	const ACTION_PARAM_NAME = 'a';
	const DEFAULT_CONTROLLER_NAME = 'Index';
	const DEFAULT_ACTION_NAME = 'index';
	
	
	private $controllerName;
	private $actionName;
	
	private static $instance = null;
	
	public static function getInstance()
	{
		if(self::$instance === null){
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	private function __construct()
	{
		$this->controllerName = self::DEFAULT_CONTROLLER_NAME;
		$this->actionName = self::DEFAULT_ACTION_NAME;
		$this->parseAction();
	}
	
	private function parseAction()
	{
		$request = General_Http::getInstance();
		$actionData = $request->getParam(self::ACTION_PARAM_NAME);
		/*$auth = HackerU_Auth::getInstance();
		
		if(!$auth->isAuth($actionData)){
			$actionData = $auth->getLoginAction();
		}else{
			if($actionData == $auth->getLoginAction()){
				$actionData = 'index/index';
			}
		}*/
		
		if($actionData){
			$aArr = explode('/', $actionData);
			if(is_array($aArr)){
				if(isset($aArr[0]) && $aArr[0]){
					$this->controllerName = ucfirst($aArr[0]);
				}
				if(isset($aArr[1]) && $aArr[1]){
					$this->actionName = $aArr[1];
				}
			}
		}
	}
	
	public function run()
	{
		$controllerName = 'Controller_'.$this->controllerName;
		$actionName = $this->actionName;
		try{
			$controller = new $controllerName($actionName);
			$controller->dispatch();
		}catch(Exception $e){
			var_dump($e->getMessage());
		}
	}
}